package com.kangfawei.io;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 对象序列化与反序列化
 */
public class ObjectStreamTest {
    public static void main(String[] args) {
        Student student = new Student();
        student.setId(1);
        student.setName("崔浩");
        student.setAge(26);
        student.setInfo("家住莱西");

        Student student2 = new Student();
        student2.setId(2);
        student2.setName("刘旭峰");
        student2.setAge(30);
        student2.setInfo("家住烟台");

        Student student3 = new Student();
        student3.setId(2);
        student3.setName("靳晓菲");
        student3.setAge(21);
        student3.setInfo("家住菏泽");
        String path = "E:/IO_target/student.txt";

        try {
            writeToFile(path,student);
            writeToFile(path,student2);
            writeToFile(path,student3);
            readFromFile(path);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


    }

    /**
     * 将文件通过追加方式写入到文件中
     * @param path
     * @param student
     * @throws IOException
     */
    public static void writeToFile(String path,Student student) throws IOException {
        OutputStream os = null;
        ObjectOutputStream oos = null;
        try {
            File file = new File(path);
            os = new FileOutputStream(file,true);
            // 文件长度小于1时，说明时第一次写入
            if(file.length() < 1){
                oos = new ObjectOutputStream(new BufferedOutputStream(os));
                oos.writeObject(student);
                oos.flush();
            }else{
                oos = new MyObjectOutputStream(new BufferedOutputStream(os));
                oos.writeObject(student);
                oos.flush();
            }

        }catch (IOException e){
            e.printStackTrace();
        }

    }

    /**
     * 从文件中读取对象
     * @param path
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static void readFromFile(String path) throws IOException, ClassNotFoundException {
        FileInputStream fis = new FileInputStream(path);
        ObjectInputStream ois = new ObjectInputStream(fis); // 注意此处不能加缓冲流
        // 循环条件中的fis不能改成ois，否则无法读出数据
        while(fis.available() > 0){
            System.out.println(ois.readObject());
        }
    }

}

class MyObjectOutputStream extends ObjectOutputStream{

    public MyObjectOutputStream(OutputStream out) throws IOException {
        super(out);
    }

    protected MyObjectOutputStream() throws IOException, SecurityException {
    }

    /**
     * 重写writeStreamHeader，如果不是第一次写入的时候，调用这个方法
     * @throws IOException
     */
    @Override
    protected void writeStreamHeader() throws IOException {
        return;
    }
}

class Student implements Serializable{

    private Integer id;
    private String name;
    private Integer age;
    private String info;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(id, student.id) &&
                Objects.equals(name, student.name) &&
                Objects.equals(age, student.age) &&
                Objects.equals(info, student.info);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, age, info);
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", info='" + info + '\'' +
                '}';
    }
}
